/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.DocProject.Sandcastle.ContentManagement
{
	public sealed partial class XmlContentDialog : Form
	{
		#region Public Properties
		public int ContentDocumentListHeight
		{
			get
			{
				return editor.DocumentListHeight;
			}
			set
			{
				editor.DocumentListHeight = value;
			}
		}

		public int ContentListWidth
		{
			get
			{
				return editor.ListWidth;
			}
			set
			{
				editor.ListWidth = value;
			}
		}

		public int ContentAttributesGridHeight
		{
			get
			{
				return editor.AttributesGridHeight;
			}
			set
			{
				editor.AttributesGridHeight = value;
			}
		}
		#endregion

		#region Private / Protected
		private readonly XmlContentDialogSettings dialogSettings;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="XmlContentDialog" /> class.
		/// </summary>
		public XmlContentDialog(XmlContentManager manager)
			: this()
		{
			if (manager == null)
				throw new ArgumentNullException("manager");

			editor.Importer = manager.Importer;
			editor.DisplayManager = manager.DisplayManager;
			editor.Documents = manager.Documents;

			dialogSettings = new XmlContentDialogSettings(this);
			dialogSettings.Load();
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="XmlContentDialog" /> class.
		/// </summary>
		private XmlContentDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
					components.Dispose();

				dialogSettings.Dispose();
			}
			base.Dispose(disposing);
		}
		#endregion

		#region Events
		private readonly object ContentDocumentListHeightChangedEvent = new object();
		private readonly object ContentListWidthChangedEvent = new object();
		private readonly object ContentAttributesGridHeightChangedEvent = new object();

		/// <summary>
		/// Event raised when the <see cref="ContentAttributesGridHeight"/> property value changes.
		/// </summary>
		[Category("Property Changed")]
		[Description("Event raised when the ContentAttributesGridHeight propety value changes.")]
		public event EventHandler ContentAttributesGridHeightChanged
		{
			add
			{
				lock (ContentAttributesGridHeightChangedEvent)
				{
					Events.AddHandler(ContentAttributesGridHeightChangedEvent, value);
				}
			}
			remove
			{
				lock (ContentAttributesGridHeightChangedEvent)
				{
					Events.RemoveHandler(ContentAttributesGridHeightChangedEvent, value);
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ContentAttributesGridHeightChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnContentAttributesGridHeightChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (ContentAttributesGridHeightChangedEvent)
			{
				handler = (EventHandler) Events[ContentAttributesGridHeightChangedEvent];
			}

			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Event raised when the <see cref="ContentListWidth"/> property value changes.
		/// </summary>
		[Category("Property Changed")]
		[Description("Event raised when the ContentListWidth property value changes.")]
		public event EventHandler ContentListWidthChanged
		{
			add
			{
				lock (ContentListWidthChangedEvent)
				{
					Events.AddHandler(ContentListWidthChangedEvent, value);
				}
			}
			remove
			{
				lock (ContentListWidthChangedEvent)
				{
					Events.RemoveHandler(ContentListWidthChangedEvent, value);
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ContentListWidthChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnContentListWidthChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (ContentListWidthChangedEvent)
			{
				handler = (EventHandler) Events[ContentListWidthChangedEvent];
			}

			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Event raised when <see cref="ContentDocumentListHeight"/> property value changes.
		/// </summary>
		[Category("Property Changed")]
		[Description("Event raised when the ContentDocumentListHeight property value changes.")]
		public event EventHandler ContentDocumentListHeightChanged
		{
			add
			{
				lock (ContentDocumentListHeightChangedEvent)
				{
					Events.AddHandler(ContentDocumentListHeightChangedEvent, value);
				}
			}
			remove
			{
				lock (ContentDocumentListHeightChangedEvent)
				{
					Events.RemoveHandler(ContentDocumentListHeightChangedEvent, value);
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ContentDocumentListHeightChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnContentDocumentListHeightChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (ContentDocumentListHeightChangedEvent)
			{
				handler = (EventHandler) Events[ContentDocumentListHeightChangedEvent];
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion

		#region Event Handlers
		protected override void OnFormClosing(FormClosingEventArgs e)
		{
			base.OnFormClosing(e);

			if (!e.Cancel)
				dialogSettings.Save();
		}

		private void editor_ListWidthChanged(object sender, EventArgs e)
		{
			OnContentListWidthChanged(e);
		}

		private void editor_DocumentListHeightChanged(object sender, EventArgs e)
		{
			OnContentDocumentListHeightChanged(e);
		}

		private void editor_AttributesGridHeightChanged(object sender, System.EventArgs e)
		{
			OnContentAttributesGridHeightChanged(e);
		}
		#endregion
	}
}